﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class RandomBackgroundElement : MonoBehaviour
{
    //定义背景元素预制体列表
    [SerializeField]
    private List<GameObject> allTypeElements;

    //存储所有背景元素实例的列表
    [SerializeField]
    private List<GameObject> allElements;

    //元素移动速度
    [SerializeField]
    private float moveSpeed;

    //元素创建间隔
    [SerializeField]
    private double createInterval;
    [SerializeField]
    private double intervalRandom;

    //元素y轴随机偏移范围
    [SerializeField]
    private float yOffsetRandom;

    //元素随机缩放范围
    [SerializeField]
    private float scaleRandom;

    //创建元素计时器
    private double creatTimer = 0;

    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //计时器归零时随机创建元素
        if(creatTimer <= 0)
        {
            creatTimer = NewInterval();

            int types = allTypeElements.Count;
            System.Random random = new System.Random();
            GameObject element = allTypeElements[random.Next(types)];

            RectTransform rectTransform = element.GetComponent<RectTransform>();
            float localScreenWidth = Screen.width / transform.lossyScale.x;
            float xPosition = localScreenWidth / 2 + rectTransform.sizeDelta.x / 2;

            float yPositionRange = (float)random.NextDouble() * yOffsetRandom * 2 - yOffsetRandom;

            Vector3 originPosition = transform.localPosition;
            Vector3 newPosition = new Vector3(xPosition, originPosition.y + yPositionRange);
            transform.localPosition = newPosition;

            GameObject newElementInstance = Instantiate(element, transform.position, Quaternion.identity, transform.parent);

            float scaleRange = (float)random.NextDouble() * scaleRandom * 2 - scaleRandom + 1;
            Vector3 originScale = newElementInstance.transform.localScale;
            Vector3 newScale = originScale * scaleRange;
            newElementInstance.transform.localScale = newScale;

            allElements.Add(newElementInstance);
        }
        creatTimer -= Time.deltaTime;

        //移动所有元素，并将超出屏幕范围的元素删除
        foreach (GameObject element in allElements)
        {
            element.transform.localPosition += new Vector3(-moveSpeed * Time.deltaTime, 0);
        }
        if (OutScreen(allElements[0]))
        {
            Destroy(allElements[0]);
            allElements.RemoveAt(0);
        }
    }

    //判断元素是否超出屏幕显示范围
    bool OutScreen(GameObject gameObject)
    {
        RectTransform rectTransform = gameObject.GetComponent<RectTransform>();
        float localScreenWidth = Screen.width / transform.lossyScale.x;

        if (Mathf.Abs(rectTransform.localPosition.x) - rectTransform.sizeDelta.x / 2 > localScreenWidth / 2)
        {
            return true;
        }
        return false;
    }

    //创建新的随机时间间隔
    double NewInterval()
    {
        System.Random random = new System.Random();
        double randomRange = random.NextDouble() * intervalRandom * 2 - intervalRandom;
        return createInterval + randomRange;
    }
}
